package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 执行localhost:8080/set后，Redis中生成数据如下：
 * "spring:session:sessions:e2004c41-6d2d-4ed0-9345-31047cc19324"
 * "spring:session:expirations:1561538460000"
 * "spring:session:sessions:expires:e2004c41-6d2d-4ed0-9345-31047cc19324"
 * <p>
 * 执行如下操作即可：
 * localhost:8080/set
 * localhost:8080/get
 * localhost:8081/get
 *
 * @author Shanks
 * @date 2019-06-26
 */
@RestController
public class HelloController {
    ThreadLocal<String> local = new ThreadLocal<>();
    @Value("${server.port}")
    private Integer port;

    @GetMapping("/set")
    public String set(HttpSession session) {
        local.remove();
        session.setAttribute("user", "Shanks");
        return String.format("setAttribute on port: %d", port);
    }

    @GetMapping("/get")
    public String get(HttpSession session) {

        return String.format("getAttribute on port: %d\t%s", port, session.getAttribute("user"));
    }
}